package com.neouton.common.util;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;


@Data
@NoArgsConstructor
@Accessors(chain = true)
public class ResponseAPI<T> {

    private String code;
    private String message;
    private T result;

    public ResponseAPI(String code) {
        this.code = code;
    }

    public ResponseAPI(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public ResponseAPI(String code, T data) {
        this.code = code;
        this.result = data;
    }

    public ResponseAPI(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.result = data;
    }

    /**
     * @description: 请求成功状态码==200
     */
    public static ResponseAPI success() {
        return new ResponseAPI().setCode("200");
    }

    public static ResponseAPI successMes(String message) {
        return new ResponseAPI("200",message);
    }

    public  ResponseAPI<T> successAnyData(T data) {
        return new ResponseAPI("200","成功",data);
    }

    public static   ResponseAPI successData(Object data) {
        return new ResponseAPI("200","成功",data);
    }

    public  ResponseAPI success(String message, T data) {
        return new ResponseAPI("200",message,data);
    }

    /**
     * @description: 请求失败状态码 code==500
     */
    public static ResponseAPI error(String message) {
        ResponseAPI response = new ResponseAPI("500", message);
        return response;
    }

    public static ResponseAPI error(String code,String message) {
        ResponseAPI response = new ResponseAPI(code, message);
        return response;
    }

    /**
     * 失败
     */
    public static ResponseAPI error(BaseErrorInfoInterface errorInfo) {
        ResponseAPI response = new ResponseAPI(errorInfo.getResultCode(),errorInfo.getResultMsg());
        return response;
    }

    public ResponseAPI setSuccess(String success) {
        if (success != null)
            this.code = success;
        return this;
    }

    public ResponseAPI setMessage(String mes) {
        if (message != null)
            this.message = mes;
        return this;
    }

    /**
     * @description: 放入任意的值
     */
    public ResponseAPI setData(T object) {
        if (object != null)
            this.result = object;
        return this;
    }
}
